package redisManager

import (
	"CaptchaCluster/cmd"
	"CaptchaCluster/log"
	"context"
	"github.com/go-redis/redis/v8"
	"time"
)

var (
	Rdb    *redis.Client
	RdbCtx = context.Background()
)

func Start() {
	log.Release("start the redis-server")
	seekOut, err := cmd.StartWithKeyword(10*time.Second, cmd.UTF8, "Ready to accept connections", "cd Redis-x64-5.0.9", "redis-server.exe")

	if err != nil {
		log.Fatal("fatal error starting redis-server:%v", err)
	} else if !seekOut {
		log.Fatal("fatal error starting redis-server")
	}
	log.Release("starting redis-server complete")

	connect()
}

func Stop() {
	Rdb.Do(RdbCtx, "shutdown")
}

func connect() {
	log.Release("连接redis")
	Rdb = redis.NewClient(&redis.Options{Addr: "localhost:6379", DB: 10})
	pong, err := Rdb.Ping(RdbCtx).Result()
	if err != nil || pong != "PONG" {
		log.Fatal("连接redis失败")
	} else {
		log.Release("连接redis成功")
	}
}
